<?php
/**
 * Created by PhpStorm.
 * User: bookcms
 * Date: 2018/7/11
 * Time: 下午8:56
 */

namespace app\common\taglib;

use think\Db;
use think\facade\Config;
use think\facade\Url;
use think\template\TagLib;

class Tag extends TagLib
{
    /**
     * 定义标签列表
     */
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'web' => ['attr' => 'name,value', 'close' => 0],
        'ad' => ['attr' => 'name', 'close' => 0],
        'articles' => ['attr' => 'cid,limit', 'close' => 1],
        'block_list' => ['attr' => 'block_id,limit', 'close' => 1],
        'cid_list' => ['attr' => 'id,page,page_size', 'close' => 1],
    ];

    public function tagAd($tag, $content) {
        $id= isset($tag['id']) ? $tag['id'] : 10;

        $parse = <<<EOF
        <?php
                \$ad = curl_server("apiv2/ad?id=" . $id);
                echo \$ad['data'];
            ?>
EOF;
        return $parse;
    }

    public function tagArticles($tag, $content)
    {
        $limit = isset($tag['limit']) ? $tag['limit'] : 10;
        $offset = isset($tag['offset']) ? $tag['offset'] : 0;
        $cid = isset($tag['cid']) ? $tag['cid'] : '';

        //分类id
        if (!empty($cid)) {
            $cid  = $this->autoBuildVar($cid);
        }

        $parse = <<<EOF
        <?php
                \$__LIST__ = curl_server("apiv2/articlelist?cid=" . $cid . "&limit=" . $limit . "&offset=" . $offset);
                if(is_array(\$__LIST__['data'])):
                   foreach(\$__LIST__['data'] as \$index => \$item):
            ?>
EOF;

        $parse .= $content;
        $parse .= '<?php endforeach; ?>';
        $parse .= '<?php endif; ?>';

        return $parse;
    }

    public function tagBlock_list($tag, $content)
    {
        $limit = isset($tag['limit']) ? $tag['limit'] : 10;
        $block_id = isset($tag['block_id']) ? $tag['block_id'] : '';

        if (!empty($block_id)) {
            $block_id  = $this->autoBuildVar($block_id);
        }

        $parse = <<<EOF
        <?php
                \$__LIST__ = curl_server("apiv2/block?id=" . "$block_id" . "&limit=" . "$limit");
                if(is_array(\$__LIST__['data'])):
                   foreach(\$__LIST__['data'] as \$index => \$item):
            ?>
EOF;

        $parse .= $content;
        $parse .= '<?php endforeach; ?>';
        $parse .= '<?php endif; ?>';

        return $parse;
    }

    public function tagCid_list($tag, $content)
    {
        $page_size = isset($tag['page_size']) ? $tag['page_size'] : 10;
        $page = isset($tag['page']) ? $tag['page'] : 1;

        $id = isset($tag['block_id']) ? $tag['id'] : '';

        if (!empty($id)) {
            $id  = $this->autoBuildVar($id);
        }

        $parse = <<<EOF
        <?php
                \$__LIST__ = curl_server("apiv2/classifylist?flag_type=cid_list&cid=" . "$id" . "&page=" . "$page" ."&page_size=" . "$page_size");
                if(is_array(\$__LIST__['data']['list'])):
                   foreach(\$__LIST__['data']['list'] as \$index => \$item):
            ?>
EOF;

        $parse .= $content;
        $parse .= '<?php endforeach; ?>';
        $parse .= '<?php endif; ?>';

        return $parse;
    }

}